package com.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by WangJJ on 16/4/28.
 */
public class DateUtil {

    public static String getTimeYearMonDayHourMinSec(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getTimeMonDayHourMinSec(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getTimeMonDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        return simpleDateFormat.format(date);
    }

    public static String getTimeYearMon(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        return simpleDateFormat.format(date);
    }

    public static String getChineseTimeYearMon(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
        return simpleDateFormat.format(date);
    }

    public static String getChineseTimeMonDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
        return simpleDateFormat.format(date);
    }

    public static String getTimeDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        return simpleDateFormat.format(date);
    }

    //"2014-06-08"
    public static String getTimeMonDay(String str) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd");
        try {
            Date date = simpleDateFormat.parse(str);
            return simpleDateFormat2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date getTimeYearMonDay(String str) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = simpleDateFormat.parse(str);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date getTimeYearMonDayHourMinute(String str) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            Date date = simpleDateFormat.parse(str);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static String getChineseTimeMonDay(String str, SimpleDateFormat simpleDateFormat) {

        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM月dd日");
        try {
            Date date = simpleDateFormat.parse(str);
            return simpleDateFormat2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getConvertTiem(String time, String oldFormat, String newFormat) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(oldFormat);

        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(newFormat);
        try {
            Date date = simpleDateFormat.parse(time);
            return simpleDateFormat2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getTimeHourMinSec(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getTimeYearMonDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    //当前时间之后i天
    public static Date getTimeAfterNow(int n) {
        Date date = new Date();
        long dateTime = 24 * 60 * 60 * 1000;
        return new Date(date.getTime() + dateTime * n);
    }

    public static Date getTimeAfterDate(Date date,int n) {
        long dateTime = 24 * 60 * 60 * 1000;
        return new Date(date.getTime() + dateTime * n);
    }


    //是否为昨天
    public static boolean isYesterDay(Date date) {
        long oneDayTime = 86400000;
        return isTheDay(date, new Date(new Date().getTime() - 86400000));
    }

    //根据日期取得星期几
    public static String getWeekAnother(Date date) {
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    //根据日期取得星期几
    public static String getWeek(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(date);
        return week;
    }

    /**
     * date是否是指定日期的天的范围内
     */
    public static boolean isTheDay(Date date, Date day) {
        return date.getTime() >= dayBegin(day).getTime()
                && date.getTime() <= dayEnd(day).getTime();
    }

    /**
     * date是否是指定日期的年的范围内
     */
    public static boolean isTheYear(final Date date, final Date year) {
        return date.getTime() >= yearBegin(year).getTime()
                && date.getTime() <= yearEnd(year).getTime();
    }

    /**
     * 获取指定时间的那天 00:00:00.000 的时间
     *
     * @param date
     * @return
     */
    public static Date dayBegin(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取指定时间的那天 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date dayEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    public static Date chineseStartOfWeek(final Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        return cal.getTime();
    }

    public static Date chineseEndOfWeek(final Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, 6);
        return cal.getTime();
    }

    /**
     * 月首
     *
     * @param date
     * @return
     */
    public static Date monthBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 月末
     *
     * @param date
     * @return
     */
    public static Date monthEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * 年首
     *
     * @param date
     * @return
     */
    public static Date yearBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 年末
     *
     * @param date
     * @return
     */
    public static Date yearEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, c.getActualMaximum(Calendar.DAY_OF_YEAR));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * 返回指定时间的下一个月.
     * @param curDate
     *            	指定的时间字符串
     * @return String
     * 				时间字符串，以 yyyy-MM-dd 格式
     */
    public static Date getNextMonth(Date curDate) {
        Calendar now = Calendar.getInstance();
        now.setTime(curDate);
        now.add(Calendar.MONTH, 1);
        return now.getTime();
    }

    /**
     * 返回指定时间的延后或前移几月的时间.
     * @param curDate 指定的时间字符串
     * @param num 延后或前移的月数
     * @return 时间字符串，以 yyyy-MM-dd 格式;
     */
    public static Date getNextMonth(Date curDate, int num) {
        Calendar now = Calendar.getInstance();
        now.setTime(curDate);
        now.add(Calendar.MONTH, num);
        return now.getTime();
    }

    /**
     * 返回指定时间的上一个月.
     * @param curDate
     *            	指定的时间字符串
     * @return String
     * 				时间字符串，以 yyyy-MM-dd 格式
     */
    public static Date getPreMonth(Date curDate) {
        Calendar now = Calendar.getInstance();
        now.setTime(curDate);
        now.add(Calendar.MONTH, -1);
        return now.getTime();
    }

    //2016-04-20~2016-04-27
    public static String getTimeYearPeriod(List<Date> dateList) {
        if (dateList == null) {
            return null;
        } else if (dateList.size() == 1) {
            return getTimeYearMonDay(dateList.get(0));
        } else {
            if (dateList.size() > 1) {
                long minDate = dateList.get(0).getTime();
                long maxDate = dateList.get(0).getTime();
                for (int i = 0; i < dateList.size(); i++) {
                    long time = dateList.get(i).getTime();
                    if (time > maxDate) maxDate = time;
                    if (time < minDate) minDate = time;
                }
                String minDateStr = getTimeYearMonDay(new Date(minDate));
                String maxDateStr = getTimeYearMonDay(new Date(maxDate));
                if (minDateStr.equals(maxDateStr))
                    return minDateStr;
                else return minDateStr + "~" + maxDateStr;
            } else return null;
        }

    }

}
